<?php

require MX_SHOP_PATH . 'core/web/mroles/page_merch.php';
class Index_MxShopPage extends MrolesWebPage {

    public function main(){

        global $_W,$_GPC;

        include $this->template('mroles/commission');
    }

    public function detail(){

        global $_W,$_GPC;

        $condition = ' AND l.uid=:uid ';
        $params = array(':uid'=>$_W['manageid']);
        $pageparams = array();

        if(!empty($_GPC['keyword'])){

            $type = $_GPC['type'];
            if($type == 'ordersn'){
                $condition .= ' AND locate(:keyword,l.ordersn)>0 ';
            }else if($type == 'goods'){
                $condition .= ' AND locate(:keyword,o.goods_name)>0 ';
            }else if($type == 'username'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0) ';
            }else{
                $condition .= ' AND locate(:keyword,o.mendian_name)>0 ';
            }
            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            $pageparams['type'] = $type;
            $pageparams['keyword'] = $_GPC['keyword'];
        }

        $pindex = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        $psize = 10;
        $list = pdo_fetchall('SELECT l.*,m.realname,m.mobile,m.avatar,o.realprice as price FROM '.tablename('mx_shop_service_commission_log').' l '.
                                    'LEFT JOIN '.tablename('mx_shop_service_order').' o ON o.ordersn=l.ordersn '.
                                    'LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid '.
                                    'WHERE 1 '.$condition.' ORDER BY id DESC '.
                                    'LIMIT '.($pindex-1) * $psize.','.$psize, $params);


        $total = pdo_fetchcolumn('SELECT count(*) FROM '.tablename('mx_shop_service_commission_log').' l LEFT JOIN '.tablename('mx_shop_service_order').' o ON o.ordersn=l.ordersn LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid WHERE 1 '.$condition, $params);
        $pager = owpager($total, $pindex, $psize, $pageparams);

        $level = array('1'=>'店主', '2'=>'创业者', '3'=>'服务商', '4'=>'运营商', '5'=>'运营商');

        $commission = pdo_fetch('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE uid=:uid', array(':uid'=>$_W['manageid']));
        if($_W['ismanage']){
            $mdids = $this->getMendianGoods(1,1);
            $mdcomm = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE mendianid in ('.implode(',', $mdids).')');
            if(!empty($mdcomm)){
                foreach ($mdcomm as $item) {
                    $commission['total'] += $item['total'];
                    $commission['remain'] += $item['remain'];
                    $commission['freeze'] += $item['freeze'];
                }
            }

        }

        include $this->template('mroles/commission/detail');
    }

    public function mddetail(){

        global $_W,$_GPC;

        if(!$_W['ismanage']) {
            $this->message('访问地址有误');
        }

        $mdids = $this->getMendianGoods(1,1);
        $mdids = implode(',', $mdids);

        $commission = array('total' => 0, 'remain' => 0, 'drawal' => 0, 'freeze' => 0);
        $mdcomm = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_commission').' WHERE mendianid in ('.$mdids.')');
        if(!empty($mdcomm)){
            foreach ($mdcomm as $item) {
                $commission['total'] += $item['total'];
                $commission['remain'] += $item['remain'];
                $commission['drawal'] += $item['drawal'];
                $commission['freeze'] += $item['freeze'];
            }
        }

        $condition = ' AND l.mendianid in ('.$mdids.') ';
        $pageparams = $params = array();

        if(!empty($_GPC['keyword'])){

            $type = $_GPC['type'];
            if($type == 'ordersn'){
                $condition .= ' AND locate(:keyword,l.ordersn)>0 ';
            }else if($type == 'goods'){
                $condition .= ' AND locate(:keyword,o.goods_name)>0 ';
            }else if($type == 'username'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0 ';
            }else{
                $condition .= ' AND locate(:keyword,o.mendian_name)>0 ';
            }

            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            $pageparams['type'] = $type;
            $pageparams['keyword'] = $_GPC['keyword'];
        }

        $pindex = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        $psize = 10;
        $list = pdo_fetchall('SELECT l.*,m.realname,m.avatar,o.realprice as price FROM '.tablename('mx_shop_service_commission_log').' l '.
            'LEFT JOIN '.tablename('mx_shop_service_order').' o ON o.ordersn=l.ordersn '.
            'LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid '.
            'WHERE 1 '.$condition.' ORDER BY id DESC '.
            'LIMIT '.($pindex-1) * $psize.','.$psize, $params);

        $total = pdo_fetchcolumn('SELECT count(*) FROM '.tablename('mx_shop_service_commission_log').' l LEFT JOIN '.tablename('mx_shop_service_order').' o ON o.ordersn=l.ordersn LEFT JOIN '.tablename('mx_shop_user').' m ON m.id=o.uid WHERE 1 '.$condition, $params);
        $pager = owpager($total, $pindex, $psize, $pageparams);

        $level = array('1'=>'店主', '2'=>'创业者', '3'=>'服务商', '4'=>'运营商', '5'=>'运营商');


        include $this->template('mroles/commission/detail');
    }

}